//index.js

import {
  getBanners,getCategory,getAtlasList,getImagesByAtlasId
} from '../../service/config/index.js'
import {BUS_STATUS,BUS_Category} from '../../service/config/constant.js'

//获取应用实例
const app = getApp()
const BACKTOPDIS=500;

Page({
  data: {
    showBacktop: false, //显示会到顶部的按钮
    banners:[],         //轮播图
    recommends:[],      //推荐
    recommendsStar:[],
    populars:[],        //流行
    titles: [],
    goods:[],
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    nvabarData: {
      title: '初见', //导航栏 中间的标题
      keyWord: '',
    },
    currentTitle: '',
    currentIndex:1,
  },
  onShow() {
      // 清除搜索框 调用子组件的清空函数
      this.selectComponent('#bar').clearInput();
      // 每个机型的尺寸不一致  
      // 在app.js中获取到的 statusBarHeight 用于控制头部的高度
      
      this.setData({
          height: app.globalData.height,
      })
      this.setData({
          'nvabarData.inputValue': ''
      })

  },
  search: function(e) {
      this.getTask(e.detail);
  },

  ItemClick(param){
    let ind = param.detail.index
    let item = this.data.recommendsStar[ind];
    console.log(this.data.recommendsStar)
    let id = param.detail.id
    let option = {}
    option.title = item['atalsName']
    option.id = id
    option.time = item['createTime']
    
    wx.navigateTo({
      url: '/pages/detail/atlasImg/atlasImg?param='+JSON.stringify(option),
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {

      let that = this;

      // 1.请求轮播图以及推荐数据
      that._getBanners();

      //请求获壁纸
      that._getAtlasList();

      //获取本周流行
      // that._getPopular();

      //获取tag
      that._getCategory();

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  
  //网络请求函数 获取banner图
  _getBanners(){
    let param = {}
    param.status = BUS_STATUS.Enable    
    param.type = BUS_Category.TYPE_Atalas
    getBanners(param).then(res=>{
      const banners = res.rows
      this.setData({
        banners
      });
    })
  },

  // _getPopular(){
  //   let param = {}
  //   param.type = 1
  //   getBanners(param).then(res=>{
  //     const popular = res.data
  //     this.setData({
  //       populars:popular
  //     });
  //   })
  // },

  
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  _getAtlasList(){
    let that = this;
    let param = {}

    //获取热度最高前10图集
    param.status = BUS_STATUS.Enable  
    param.size = 10
    getAtlasList(param).then(res=>{
      const recommends = res.rows
      that.setData({
        recommends:recommends
      });
    })
  },

  // 获取Tag数据
  _getCategory(){
    let that = this;
    let param = {}
    param.type = BUS_Category.TYPE_Atalas
    param.status = BUS_STATUS.Enable  
    getCategory(param).then(res=>{
      let nav = res.rows
      nav.unshift({id:0,name:"全部"})
      that.setData({
        titles:nav,
        currentIndex:nav[0].id
      })
    })

    // 加载完导航后 默认显示全部非壁纸的
    param = {}
    param.status = 1
    param.size = 50
    getAtlasList(param).then(res=>{
      if(res.code == 200){
        console.log(res.rows)
        // 设置currentType
        that.setData({
          recommendsStar:res.rows
        })
      }
    })
  },



  handleTabClick(event) {
    let that = this
    // 取出atlasType
    const atlasType = event.detail.index;
    let param = {}
    param.cateId = atlasType
    param.status = 1
    getAtlasList(param).then(res=>{
      if(res.code == 200){

        // 设置currentType
        that.setData({
          recommendsStar:res.rows
        })
      }
    })

  },
  onPageScroll: function(options){
    const scrollTop = options.scrollTop;
    //回到顶部
    const flag=scrollTop >=BACKTOPDIS

    //官方提示，不建议在onPageScroll 频繁的调用this.setData方法
    
    //因为会不断的刷新界面
    if(flag!=this.data.showBacktop){
      this.setData({
        showBacktop:flag
      })
    }
 
    //tabcontrol停留效果
    const flag2=scrollTop>=this.data.tabScrollTop;
    if(flag2!=this.data.isFiexd){
      this.setData({
        isFiexd:flag2
      })
    }
  },


  /**
   * 
   * @param {点击推荐数据跳转} param 
   */
  ItemClickToType(param){
    let ind = param.detail.index
    let item = this.data.recommends[ind];
    console.log(item)
    let id = param.detail.id
    let option = {}
    option.title = item['adTitle']
    option.id = id
    option.time = item['createTime']
    wx.navigateTo({
      url: '/pages/detail/type/type?param='+JSON.stringify(option),
    })
  },

  //goods item点击页面跳转
  goodsItemClick(event){
    const index=event.detail.index;
    console.log(this.data);
    const id = this.data.goods[index].id
    //跳转页面
    wx.navigateTo({
      url: '/pages/detail/detail?id=' +id,
    })
  },  

   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket:true,
      menu:['shareAppMessage','shareTimeline']
    })
 
    return {
      title:'里面的壁纸好好看哦',
      imageUrl:''
    }
  },
  //用户点击右上角分享朋友圈
  onShareTimeline:function(){
    return {
      title:'里面的壁纸好好看哦',
      query:{
        key:value
      },
      imageUrl:''
    }
  },
})
